<?php

namespace Admin\Controller;
use Think\Controller;
use Admin\Model\UserModel;
use Admin\Model\AuthModel;
/**
 * @author wangnaixin
 * @todo 登录模块
 * @menu  
 * @menuAuto login  
 *
 */
class LoginController extends Controller {
    //put your code here
    public $user;
    public $auth;
    
            
    function __construct() {
        parent::__construct();
        $this->user = new UserModel();
        $this->auth = new AuthModel();
    }
    
    
    /**
     * @todo 后台用户登录页面
     */
    public function login(){
        session("userId",null);
        session("userInfo",null);
        session('default_module', null);
        session('sys_left_logo',null);
        $this->setSysPageTitle();
        $this->display();
    }
    
    /**
     * @todo 后台用户登录操作
     */
    public function doLogin(){
        $userName = I('userName','');
        $passWord = I('passWord','123456');
        $data = $this->user->doLogin($userName,$passWord);
        //dump($data);
        if($data !== false){
            //检查用户权限
            if($data['role']['back_login']){
                if (!$data['module'] || ($data['module'] && $data['module'] != 'Space')) {
                    $isMerge = true;
                    if ($data['role']['id'] == C('PIANQU_ADMIN_ROLE_ID')){
                        $isMerge = false;
                    }
                }
                //处理菜单
                if($data['role']['super'] == 1){
                    $data['menuList'] = menuInit($this->auth->where("isMenu=1")->select());
                }else{
                    $data['menuList'] = menuInit($data['auths']);
                }
                $data['authList'] = authInit($data['auths']);
                unset($data['auths']);
                session("userId",$data['id']);
                session("userInfo",$data);
                $this->ajaxReturn(true);
            }else{
                $this->ajaxReturn(false);
            }
        }else{
            $this->ajaxReturn(false);
        }   
    }

    /**
     * 系统后台标题设置
     * @return mixed|string
     */
    protected function setSysPageTitle() {
        $sysPageTitle = '';
        $curController = explode('/',C('DEFAULT_CONTROLLER'));
        $curController = array_shift($curController);
        switch ($curController) {
            case 'Building':
                $sysPageTitle = C('BUILDING_PAGE_TITLE');
                break;
            case 'Space':
                $sysPageTitle = C('SPACE_PAGE_TITLE');
                break;
        }
        $this->assign('sysPageTitle',$sysPageTitle);
    }
}
